<?php

    function F($file){
        return file_exists($file);
    }




    function T_js(){
        T_js_kv();
        T_js_file();

    }
    function T_js_file($file = ''){
        static $_file_array;
        if($file == '' && count($_file_array)!= 0){

            foreach($_file_array as $file){
                if(F(APP_ROOT.APP_JS_DIR_NAME.$file)) {
                    $tmp_file = APP_JS_DIR_NAME.$file;
                    echo <<<EOF
<script type="text/javascript" src="$tmp_file"></script>

EOF;

                }
            }
        }else{
            $_file_array[] = $file;    
        }
    }
    function T_js_kv($key = '' , $value=''){

        static $_kv_array;
        if($key == '' && count($_kv_array) !=0){
            echo <<<EOF
<script type="text/javascript">

EOF;
            foreach($_kv_array as $k => $v){
                echo <<<EOF
var $k = '$v';

EOF;
            }

            echo <<<EOF
</script>

EOF;
        }else{
            $_kv_array[$key] = $value;
        }
    }


    /**
    * 统一负责生成URL，以方便rewirte后，url改写
    * 
    * @param mixed $module
    * @param mixed $action
    * @param mixed $do
    * @param mixed $array
    */
    function U($controler = '' , $action = '', $array = array()){
        if(!defined('APP_WWWROOT')){
            die('APP_WWWROOT not defined!');
        }
        /*
        $query = TIRI_QUERY::getInstance();
        $controler = $query->controler;
        $action = $query->action;
        $get = $query->get;
        */
        $app = TIRI_APP::getInstance();
        $cname = $app->getConfig('FW_SELF','CONTROLER_NAME');
        $aname = $app->getConfig('FW_SELF','ACTION_NAME');

        if($controler == ''){
            $controler  = $app->getConfig('FW_SELF','CONTROLER');
        }
        if($action == ''){
            $action = $app->getConfig('FW_SELF','ACTION');
        }

        if($app->getConfig('FW_SELF','ENABLE_URL_REWRITE')){
            $url = $controler.'/'.$action;
        }else{
            $url = 'index.php?'.$cname.'='.$controler.'&'.$aname.'='.$action;
        }
        foreach($array as $k => $v){
            $url .= "&$k=$v";
        }
        return APP_WWWROOT.$url;
    }

    function R($url){
        header('Location:'.$url);
        exit;
    }
    /**
    * 获取一个数据库访问对象
    * 
    */
    function getdb(){
        $app = TIRI_APP::getInstance();
        $db = $app->loadService('mysql');
        $db->connect(
        $app->getConfig('dbHost'),
        $app->getConfig('dbUser'),
        $app->getConfig('dbPass'),
        $app->getConfig('dbName')
        );
        return $db;
    }



    function getMicroTime()
    {
        if(function_exists("microtime"))
        {
            list($usec, $sec) = explode(" ",microtime());         return $usec + $sec;
        } else {
            return time();
        }
    }

    function C() {
        $app = TIRI_APP::getInstance();
        $arg_nums = func_num_args();

        $config = $app->getConfig();
        if($arg_nums == 0)
            return $config;

        $config = &$config[func_get_arg(0)];
        for($i = 1; $i<$arg_nums;$i++){
            $config = &$config[func_get_arg($i)];
            if(empty($config)){
                return false;
            }
        }
        return $config;
    }

    function L($key){
        global $_L;
        echo $_L[$key];
    }
    function RL($key){
        global $_L;
        return $_L[$key];
    }
    
    
    
    function hook_in($anchor,$function){
        $app = TIRI_APP::getInstance();
        $app -> add_hook($anchor,$function);
    }

?>
